package com.example.controller;

import com.example.common.Result;
import com.example.entity.Room;
import com.example.service.PricingService;
import com.example.service.RoomService;
import jakarta.annotation.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/pricing")
public class PricingController {

    @Resource
    private PricingService pricingService;
    
    @Resource
    private RoomService roomService;
    
    /**
     * 获取指定日期的房间价格
     */
    @GetMapping("/roomPrice/{roomId}")
    public Result getRoomPrice(
            @PathVariable Long roomId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        
        // 获取房间信息
        Room room = roomService.selectById(roomId);
        if (room == null) {
            return Result.error("404", "房间不存在");
        }
        
        // 计算价格
        BigDecimal price = pricingService.calculateRoomPrice(roomId, date, room);
        
        Map<String, Object> data = new HashMap<>();
        data.put("roomId", roomId);
        data.put("roomType", room.getRoomType());
        data.put("date", date);
        data.put("basePrice", room.getPrice());
        data.put("adjustedPrice", price);
        
        return Result.success(data);
    }
    
    /**
     * 获取日期范围内的房间价格
     */
    @GetMapping("/dateRangePrices/{roomId}")
    public Result getDateRangePrices(
            @PathVariable Long roomId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        
        // 检查参数
        if (startDate.isAfter(endDate)) {
            return Result.error("400", "开始日期不能晚于结束日期");
        }
        
        // 计算价格
        Map<LocalDate, BigDecimal> priceMap = pricingService.calculateDateRangePrices(roomId, startDate, endDate);
        
        return Result.success(priceMap);
    }
} 